<?php

/**
 * @category   Oos
 * @package    Oos_Site
 * @license    New BSD License
 */

/**
 * Zend_Gdata_Query
 */
require_once('Zend/Gdata/Query.php');

class Oos_Site_SiteQuery extends Zend_Gdata_Query
{

    protected $_defaultFeedUri = 'http://11870.com/api/v1/search';
    protected $_queryText = null;
    protected $_tags = null;
    protected $_tagop = 'and';
    protected $_swpoint = null;
    protected $_nepoint = null;
    protected $_centerpoint = null;
    protected $_radius = null;
    protected $_areaSlug = null;
    protected $_lang = null;
    protected $_entrypoint = null;
    protected $_user_slug = null;
    protected $_location = null;


    public function __construct($entrypoint)
    {
        parent::__construct();
        $this->_entrypoint = $entrypoint;
    }

    public function setUserSlug($value) {
    	$this->_user_slug = $value;
    }
    
    public function setApiKey($value) {
    	$this->_apiKey = $value;
    }
    
    public function setTagOp($value) {
    	if ($value != 'and' && $value != 'or')
    		throw new Zend_Exception('Tag operator can only be "or"/"and"');
    	$this->_tagop = $value;
    }
    
    public function setLang($value) {
    	$this->_lang = value;
    }
    
    public function setLocation($value) {
    	$this->_location = $value;
    }
    
    public function setAreaType($value) {
    	$this->_areaType = $value;
    }
    
    public function setAreaSlug($value) {
    	$this->_areaSlug = $value;
    }
    
    public function setNEPoint($value) {
    	$this->_nepoint = $value;
    }
    
    public function setSWPoint($value) {
    	$this->_swpoint = $value;
    }
    
	public function setTags($value) {
		$this->_tags = $value;
	}
    
	public function setQueryText($value) {
		$this->_queryText = $value;
	}
    
	public function setCenterPoint($value) {
		$this->_centerpoint = $value;
	}
	
	public function setRadius($value) {
		$this->_radius = $value;
	}
	
    public function getQueryUrl() {
        $uri = $this->_defaultFeedUri;

        if ($this->_areaSlug != null) {
        	$this->_params['as'] = $this->_areaSlug;
        }
        
        if ($this->_swpoint != null && $this->_nepoint != null) {
        	$sw = split(",", $this->_swpoint);
        	$ne = split(",", $this->_nepoint);
        	$this->_params['bbox'] = $sw[1] . "," . $sw[0] . "," . $ne[1] . "," . $ne[0];
        }
        
        if ($this->_centerpoint != null && $this->_radius != null) {
        	$coords = split(",", $this->_centerpoint);
        	
        	$this->_params['lat'] = $coords[0];
        	$this->_params['lon'] = $coords[1];
        	$this->_params['radius'] = $this->_radius;
        }
        
        if ($this->_location != null) {
        	$this->_params['l'] = $this->_location;
        }
        
        if ($this->_queryText != null) {
        	$this->_params['q'] = $this->_queryText;
        }

        if ($this->_tagop != null) {
        	$this->_params['tagop'] = $this->_tagop;
        }

     
        $uri .= $this->getQueryString();

        $tagarray = array();
        
        if (is_array($this->_tags))
	        foreach ($this->_tags as $tag) 
			{
	        	$tagarray[] = 'tag=' . urlencode($tag);
	        }
        
        $uri .= '&' . implode('&', $tagarray);
        
        return $uri;
    }
    
    public function getQueryString() {
        return parent::getQueryString();
    }
}


?>